//
//  JSMStrategyContinentAllViewController.m
//  TakeYouTourTogether
//
//  Created by king on 15/12/6.
//  Copyright © 2015年 king. All rights reserved.
//

#import "JSMStrategyContinentAllViewController.h"
#import "JSMStrategyContinentAllCollectionViewCell.h"
#import "JSMStrategyContinentAllModel.h"
#import "JSMStrategyInfoViewController.h"

@interface JSMStrategyContinentAllViewController ()<UICollectionViewDataSource, UICollectionViewDelegate>

@property(nonatomic, strong) BaseCollectionView *myCollectionView;
@property(nonatomic, strong) NSArray *dataArray;

@property(nonatomic, strong) UIView *errorView;

@end

@implementation JSMStrategyContinentAllViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = COLOR(225, 214, 184, 1.0);
    self.automaticallyAdjustsScrollViewInsets = NO;
    [self requestDataFromNetWorking];
}
#pragma mark - Method myCollectionView
- (BaseCollectionView *)myCollectionView {
    if (!_myCollectionView) {
//        self.automaticallyAdjustsScrollViewInsets = NO;
        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
        if (self.dataArray.count == 1) {
            flowLayout.itemSize = CGSizeMake(SCREEN_WIDTH, SCREEN_WIDTH/3*2);
        } else if (self.dataArray.count == 2) {
            flowLayout.itemSize = CGSizeMake(SCREEN_WIDTH/2, SCREEN_WIDTH/3*2);
        } else if (self.dataArray.count == 3) {
            flowLayout.itemSize = CGSizeMake(SCREEN_WIDTH/3, SCREEN_WIDTH/3);
        } else if (self.dataArray.count == 4) {
            flowLayout.itemSize = CGSizeMake(SCREEN_WIDTH/2, (SCREEN_HEIGHT-64)/2);
        } else {
            flowLayout.itemSize = CGSizeMake(SCREEN_WIDTH/2, (SCREEN_HEIGHT-64)/(ceilf(self.dataArray.count/2.0)));
        }
        flowLayout.minimumInteritemSpacing = 0;
        flowLayout.minimumLineSpacing = 0;
        flowLayout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
        _myCollectionView = [[BaseCollectionView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT) collectionViewLayout:flowLayout];
        _myCollectionView.backgroundColor = [UIColor clearColor];
        [self.view addSubview:_myCollectionView];
        _myCollectionView.showsHorizontalScrollIndicator = NO;
        _myCollectionView.showsVerticalScrollIndicator = NO;
        _myCollectionView.delegate = self;
        _myCollectionView.dataSource = self;
        [_myCollectionView registerClass:[JSMStrategyContinentAllCollectionViewCell class] forCellWithReuseIdentifier:@"strategyContinentAllCollectionViewResue"];
    }
    return _myCollectionView;
}
#pragma mark - Method
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.dataArray.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    JSMStrategyContinentAllCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"strategyContinentAllCollectionViewResue" forIndexPath:indexPath];
    cell.tempModel = self.dataArray[indexPath.row];
    return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    JSMStrategyInfoViewController *controller = [[JSMStrategyInfoViewController alloc] init];
    controller.tempModel = self.dataArray[indexPath.row];
    [self.navigationController pushViewController:controller animated:YES];
}
#pragma mark - Method requestDataFromNetWorking 网络请求数据
- (void)requestDataFromNetWorking {
    // url 没改
    NSString *strURL = [NSString stringWithFormat:@"http://api.breadtrip.com/destination/place/3/%@/", self.tempModel.vid];
    //    NSLog(@"%@", strURL);
    [AFNetworkingTool getUrl:strURL hudSuperView:self.view result:JSON success:^(id result) {
        NSDictionary *dic = result;
        NSLog(@"%@", dic);//elements data
        self.dataArray = [JSMStrategyContinentAllModel baseModelByArr:dic[@"hottest_sites"]];
        // tableView 刷新
        [self.errorView removeFromSuperview];
        [self.myCollectionView reloadData];
        
    } failure:^(NSError *error) {
        [self.navigationController popViewControllerAnimated:YES];
    }];
}
#pragma mark - Method 
- (void)refreshingData:(UIButton *)button {
    [self requestDataFromNetWorking];
}
#pragma mark - Method didReceiveMemoryWarning
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
